package com.tisson.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.tisson.database.FormatUtil;
import com.tisson.database.StatementUtil;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.AcDistrict;

public class AcDistrictDAO {
	
	private String sqlStr;
	
	public AcDistrict findById(String id) throws SQLException {
		Statement stmt = StatementUtil.getStatement();
		sqlStr = "select * from ac_district where DistrictId = '" + id + "'";
		ResultSet rs = stmt.executeQuery(sqlStr);
		AcDistrict acDistrict = new AcDistrict();
		while (rs.next()) {
			acDistrict.setDistrictId(FormatUtil.ConvertUuid(rs.getBytes("DistrictId")));
			acDistrict.setDistrictName(rs.getString("DistrictName"));
			acDistrict.setDistrictLocation(rs.getString("DistrictLocation"));
			acDistrict.setDistrictComment(rs.getString("DistrictComment"));
			acDistrict.setAreaId(FormatUtil.ConvertUuid(rs.getBytes("AreaId")));
		}
		rs.close();
		stmt.close();
		return acDistrict;
	}
	
	public List findByAreaId(String areaId)throws SQLException{
		List acDistrictList = new ArrayList();
		Statement stmt = StatementUtil.getStatement();
		sqlStr = "select * from ac_district where AreaId = '" + areaId + "'";
		ResultSet rs = stmt.executeQuery(sqlStr);
		while (rs.next()) {
			AcDistrict acDistrict = new AcDistrict();
			acDistrict.setDistrictId(FormatUtil.ConvertUuid(rs.getBytes("DistrictId")));
			acDistrict.setDistrictName(rs.getString("DistrictName"));
			acDistrict.setDistrictLocation(rs.getString("DistrictLocation"));
			acDistrict.setDistrictComment(rs.getString("DistrictComment"));
			acDistrict.setAreaId(FormatUtil.ConvertUuid(rs.getBytes("AreaId")));
			acDistrictList.add(acDistrict);
		}
		rs.close();
		stmt.close();
		return acDistrictList;
	}
	
	public void save (AcDistrict acDistrict)throws SQLException{
		Statement stmt = StatementUtil.getStatement();
		sqlStr = "INSERT INTO ac_district ("
			   + " DistrictId,"
			   + " DistrictName,"
			   + " DistrictLocation,"
			   + " DistrictComment,"
			   + " AreaId)"
			   + " values("
			   + "'" +acDistrict.getDistrictId() +"',"
			   + "'" +acDistrict.getDistrictName() +"',"
			   + "'" +acDistrict.getDistrictLocation() +"',"
			   + "'" +acDistrict.getDistrictComment() +"',"
			   + "'" +acDistrict.getAreaId() +"')";
		stmt.executeUpdate(sqlStr);
		stmt.close();
	}
	
	public void update (AcDistrict acDistrict)throws SQLException{
		Statement stmt = StatementUtil.getStatement();
		sqlStr = "update ac_district set" 
			   + " DistrictName = '"
			   + acDistrict.getDistrictName() +"',"
			   + " DistrictLocation = '"
			   + acDistrict.getDistrictLocation()+"',"
			   + " DistrictComment = '"
			   + acDistrict.getDistrictComment()+"',"
			   + " AreaId = '"
			   + acDistrict.getAreaId()+"'"
			   + " WHERE DistrictId = '"
			   + acDistrict.getDistrictId()+"'";
		stmt.executeUpdate(sqlStr);
		stmt.close();
	}
	
	public void delete(AcDistrict acDistrict) throws SQLException {
		Statement stmt = StatementUtil.getStatement();
		sqlStr = "delete from ac_district where  DistrictId= '"
				+ acDistrict.getDistrictId() + "'";
		stmt.executeUpdate(sqlStr);
		stmt.close();
	}
	
	public List findAll()throws SQLException{
		List acDistrictList = new ArrayList();
		Statement stmt = StatementUtil.getStatement();
		sqlStr = "select * from ac_district";
		ResultSet rs = stmt.executeQuery(sqlStr);
		while (rs.next()) {
			AcDistrict acDistrict = new AcDistrict();
			acDistrict.setDistrictId(FormatUtil.ConvertUuid(rs.getBytes("DistrictId")));
			acDistrict.setDistrictName(rs.getString("DistrictName"));
			acDistrict.setDistrictLocation(rs.getString("DistrictLocation"));
			acDistrict.setDistrictComment(rs.getString("DistrictComment"));
			acDistrict.setAreaId(FormatUtil.ConvertUuid(rs.getBytes("AreaId")));
			acDistrictList.add(acDistrict);
		}
		rs.close();
		stmt.close();
		return acDistrictList;
	}
	

}
